Get started with RabbitMQ on Android (Android Studio)

您所在的位置:网站首页 rabbitmq android Get started with RabbitMQ on Android (Android Studio)

Get started with RabbitMQ on Android (Android Studio)

#Get started with RabbitMQ on Android (Android Studio)| 来源: 网络整理| 查看: 265

You can confirm that the libs has been added as library by opening build.gradle and check under dependencies, all files should seen be there.

dependencies { ... compile files('libs/rabbitmq-client.jar') ... }

NOTE: Only if you are using Android Gradle plugin 0.7.0 and do get the error "Duplicate files copied in APK" when you later run your application, you need to add packagingOptions to your build.gradle file as specified in here.

android { packagingOptions { exclude 'META-INF/LICENSE.txt' exclude 'META-INF/NOTICE.txt' } } 3. Android Manifest, internet permission

We need to tell the Android system that this app is allowed to access internet. Open the AndroidManifest.xml file, located in the root of the project. Add the user permission android.permission.INTERNET just before the closing /manifest tag.

...... 4. Start coding Layout

Create the view for the application. The .xml layout file can be found under res->layout. What we have here is a root ScrollView containing a

EditText a Button and a TextView The EditText will be used as an input field for the text that will be sent. The text will be published when the button is pressed and all messages received by the subscriber will be printed to the TextView. String>(); void publishMessage(String message) { try { Log.d("","[q] " + message); queue.putLast(message); } catch (InterruptedException e) { e.printStackTrace(); } }

Create a setup function for the ConnectionFactory The connection factory encapsulates a set of connection configuration parameters, in this case the CLOUDAMQP_URL. The URL can be found in the control panel for your instance.

ConnectionFactory factory = new ConnectionFactory(); private void setupConnectionFactory() { String uri = "CLOUDAMQP_URL"; try { factory.setAutomaticRecoveryEnabled(false); factory.setUri(uri); } catch (KeyManagementException | NoSuchAlgorithmException | URISyntaxException e1) { e1.printStackTrace(); }

Create a publisher that publish messages from the internal queue. Messages are added back to the queue if an exception is catched. The publisher will try to reconnect every 5 seconds if the connection is broken.

A thread ("background" or "worker" threads or use of the AsyncTask class) is needed when we have operations to perform that are not instantaneous, such as network access when connecting to rabbitMQ.

We will use a fanout exchange. A fanout exchange routes messages to all of the queues that are bound to it and the routing key is ignored. If N queues are bound to a fanout exchange, will a new message that is published to that exchange, be copied and delivered to all N queues. Fanout exchanges are ideal for the broadcast routing of messages.

public void publishToAMQP() { publishThread = new Thread(new Runnable() { @Override public void run() { while(true) { try { Connection connection = factory.newConnection(); Channel ch = connection.createChannel(); ch.confirmSelect(); while (true) { String message = queue.takeFirst(); try{ ch.basicPublish("amq.fanout", "chat", null, message.getBytes()); Log.d("", "[s] " + message); ch.waitForConfirmsOrDie(); } catch (Exception e){ Log.d("","[f] " + message); queue.putFirst(message); throw e; } } } catch (InterruptedException e) { break; } catch (Exception e) { Log.d("", "Connection broken: " + e.getClass().getName()); try { Thread.sleep(5000); //sleep and then try again } catch (InterruptedException e1) { break; } } } } }); publishThread.start(); }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3